package com.insigma.business.qrcode.pay.bff.impl;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.insigma.business.common.dto.CurrentUser;
import com.insigma.business.common.utils.CurrentUserUtil;
import com.insigma.business.qrcode.common.converter.CareWelfareMerchantDiscountProviderConverter;
import com.insigma.business.qrcode.common.converter.CareWelfareMerchantEvaluationProviderConverter;
import com.insigma.business.qrcode.common.converter.TrainingVideoProviderConverter;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.util.RpcContextUtil;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.SessionSoldier;
import com.insigma.business.qrcode.job.dto.TrainingVideoDTO;
import com.insigma.business.qrcode.job.dto.search.TrainingVideoBSearcher;
import com.insigma.business.qrcode.job.entity.TrainingVideoDO;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantInfoSearchRequest;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantInfoUpdateRequest;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantDiscountDO;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantInfoDO;
import com.insigma.business.qrcode.merchant.service.CareWelfareMerchantInfoService;
import com.insigma.business.qrcode.pay.bff.OrderBFFProvider;
import com.insigma.business.qrcode.pay.dto.MerchantOrderBSearcher;
import com.insigma.business.qrcode.pay.dto.MerchantOrderDTO;
import com.insigma.business.qrcode.pay.dto.MerchantOrderProviderConverter;
import com.insigma.business.qrcode.pay.dto.request.MerchantOrderInsertRequest;
import com.insigma.business.qrcode.pay.dto.request.MerchantOrderSearchRequest;
import com.insigma.business.qrcode.pay.dto.request.MerchantOrderUpdateRequest;
import com.insigma.business.qrcode.pay.entity.MerchantOrder;
import com.insigma.business.qrcode.pay.service.MerchantOrderService;
import com.insigma.business.qrcode.soldier.entity.SoldierBasicInfoDO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.service.SoldierBasicInfoService;
import com.insigma.business.qrcode.welfare.dto.AdminAreaCodeEnum;
import com.insigma.business.qrcode.welfare.dto.CareWelfareMerchantDiscountDTO;
import com.insigma.business.qrcode.welfare.dto.CareWelfareMerchantInfoDTO;
import com.insigma.business.qrcode.welfare.dto.MerchantTypeEnum;
import com.insigma.framework.util.ObjectUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderBFFProviderImpl extends BaseProviderImpl implements OrderBFFProvider {

    @Resource
    private MerchantOrderService merchantOrderService;

    @Resource
    private CareWelfareMerchantInfoService careWelfareMerchantInfoService;

    @Resource
    private SoldierBasicInfoService soldierBasicInfoService;

    @Override
    public BaseResponse<CareWelfareMerchantInfoDTO> getOrderDetail(BaseGetRequest baseGetRequest) {
        return null;
    }

    @Override
    public BaseResponse<Integer> orderUpdate(MerchantOrderUpdateRequest request) {
        return null;
    }

    @Override
    public BaseResponse<String> orderPay(MerchantOrderInsertRequest insertRequest) {

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增MerchantOrder的请求参数不能为空");
                BizCheckUtil.checkNotBlank(insertRequest.getMerchantId(), "商家id请求参数不能为空");
                BizCheckUtil.checkNotBlank(insertRequest.getSoldierId(), "军人id请求参数不能为空");
                BizCheckUtil.checkEqual(insertRequest.getSoldierId(),CurrentUserUtil.getCurrentUser().getSoldierId(),"传入SoldierId和登录用户不一致");
            }
            @Override
            public void executeService() {
                final MerchantOrder merchantOrder = MerchantOrderProviderConverter.convertRequestIO2DO(insertRequest);
                CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                merchantOrder.setCreateDatetime(new Date());
                merchantOrder.setCreatorId(currentUser.getUserId());
                merchantOrder.setCreatorType(currentUser.getUserType());
                merchantOrder.setModifyDatetime(new Date());
                merchantOrder.setModifierId(currentUser.getUserId());
                merchantOrder.setModifierType(currentUser.getUserType());
                merchantOrder.setIsComment(false);
                merchantOrder.setIsConfirm(false);
                CareWelfareMerchantInfoDO careWelfareMerchantInfoDO = careWelfareMerchantInfoService.get(insertRequest.getMerchantId());
                merchantOrder.setMerchantLogo(careWelfareMerchantInfoDO.getLogo());
                merchantOrder.setMerchantType(careWelfareMerchantInfoDO.getType());
                if (currentUser != null && StringUtils.isNotBlank(currentUser.getSoldierId())) {
                    insertRequest.setSoldierId(currentUser.getSoldierId());
                }
                SoldierBasicInfoDO soldierBasicInfoDO = soldierBasicInfoService.getBySoldierId(insertRequest.getSoldierId());
                if (ObjectUtils.isNotEmpty(soldierBasicInfoDO)) {
                    merchantOrder.setSoldierName(soldierBasicInfoDO.getRealName());
                }
                int id = merchantOrderService.insert(merchantOrder);
                ResponseUtils.setupSuccessResponse(baseResponse, id+"");
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_EVALUATION_INSERT;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<String> orderConfirm(BaseGetRequest baseGetRequest) {
        BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "确认的订单请求参数不能为空");
                BizCheckUtil.checkNotNull(baseGetRequest.getId(), "确认的订单请求ID参数不能为空");
            }

            @Override
            @Transactional(rollbackFor = Exception.class)
            public void executeService() {
                merchantOrderService.confirm((String) baseGetRequest.getId());
                ResponseUtils.setupSuccessResponse(baseResponse, "确认成功");
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });

        return baseResponse;
    }

    @Override
    public BasePagingResponse<List<MerchantOrderDTO>> orderSearch(MerchantOrderSearchRequest searchRequest) {
        final BasePagingResponse<List<MerchantOrderDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "请求不能为空");
            }

            @Override
            @Transactional(rollbackFor = Exception.class)
            public void executeService() {
                CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                if (currentUser != null && StringUtils.isNotBlank(currentUser.getSoldierId())) {
                    searchRequest.setSoldierId(currentUser.getSoldierId());
                }
                final MerchantOrderBSearcher merchantOrderBSearcher = MerchantOrderProviderConverter.convertRequestSO2Searcher(searchRequest);
                PageHelper.startPage(searchRequest.getPageNo(), searchRequest.getPageSize());
                final List<MerchantOrder> merchantOrders = merchantOrderService.search(merchantOrderBSearcher);
                PageInfo<MerchantOrder> pageInfo = new PageInfo<>(merchantOrders);
                final List<MerchantOrderDTO> merchantOrderDTOS = MerchantOrderProviderConverter.convertResponseDOList2DTOList(merchantOrders);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, merchantOrderDTOS, searchRequest,pageInfo.getTotal());

            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });
        return basePagingResponse;
    }

    @Override
    public BaseResponse<MerchantOrderDTO> orderTotal(MerchantOrderSearchRequest searchRequest) {
        BaseResponse<MerchantOrderDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest.getMerchantId(), "商店ID参数不能为空");
            }

            @Override
            @Transactional(rollbackFor = Exception.class)
            public void executeService() {
                MerchantOrder merchantOrder = merchantOrderService.orderTotal(searchRequest.getMerchantId());
                ResponseUtils.setupSuccessResponse(baseResponse, MerchantOrderProviderConverter.convertResponseDO2DTO(merchantOrder));
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });

        return baseResponse;
    }

}
